package cn.com.chengmandian.bike.service.impl;

import cn.com.chengmandian.bike.model.EbMemberInfo;
import cn.com.chengmandian.bike.model.EbMemberRegister;
import cn.com.chengmandian.bike.mapper.EbMemberRegisterMapper;
import cn.com.chengmandian.bike.service.IEbMemberInfoService;
import cn.com.chengmandian.bike.service.IEbMemberRegisterService;
import cn.com.chengmandian.bike.service.IEbMemberWalletService;
import cn.com.chengmandian.core.common.constants.CommonConstant;
import cn.com.chengmandian.service.bike.constants.MemberMoneyBusinessTypeEnum;
import cn.com.chengmandian.service.bike.constants.MemberMoneyOperationTypeEnum;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 * 电单车运营-预注册有礼 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2022-09-13
 */
@Slf4j
@Service
public class EbMemberRegisterServiceImpl extends ServiceImpl<EbMemberRegisterMapper, EbMemberRegister> implements IEbMemberRegisterService {

    @Autowired
    private IEbMemberInfoService memberInfoService;
    @Autowired
    private IEbMemberWalletService memberWalletService;

    @Override
    public void checkRegister(Integer memberId) {
        EbMemberInfo ebMemberInfo = memberInfoService.getById(memberId);
        if(ObjectUtil.isNull(ebMemberInfo)){
            return;
        }
        EbMemberRegister memberRegister = this.getOne(new LambdaQueryWrapper<EbMemberRegister>()
                .eq(EbMemberRegister::getPhone,ebMemberInfo.getMobile())
                .last("limit 0,1"));
        if(ObjectUtil.isNull(memberRegister) || memberRegister.getId() <= 0){
            return;
        }
        LambdaUpdateWrapper<EbMemberInfo> memberInfoLambdaUpdateWrapper = new LambdaUpdateWrapper<EbMemberInfo>()
                .eq(EbMemberInfo::getId,ebMemberInfo.getId());
        Integer manualAuth = CommonConstant.ZERO;
        if(StrUtil.isNotBlank(memberRegister.getName()) && StrUtil.isNotBlank(memberRegister.getIdCard())){
            manualAuth = CommonConstant.ONE;
            memberInfoLambdaUpdateWrapper.set(EbMemberInfo::getIdCard, memberRegister.getIdCard());
            memberInfoLambdaUpdateWrapper.set(EbMemberInfo::getSex,IdcardUtil.getGenderByIdCard(memberRegister.getIdCard()));
            memberInfoLambdaUpdateWrapper.set(EbMemberInfo::getName,memberRegister.getName());
        }
        memberInfoLambdaUpdateWrapper.set(EbMemberInfo::getManualAuth,manualAuth);
        if(memberRegister.getIsFree().equals(CommonConstant.ONE)){
            memberInfoLambdaUpdateWrapper.set(EbMemberInfo::getLevel,CommonConstant.TWO);
        }
        memberInfoService.update(memberInfoLambdaUpdateWrapper);

        memberWalletService.updateBalanceForBoth(ebMemberInfo.getAreaId(),
                ebMemberInfo.getId(),
                memberRegister.getChargeMoney(),
                memberRegister.getGiftMoney(),
                MemberMoneyBusinessTypeEnum.REGISTER,
                memberRegister.getId(),
                MemberMoneyOperationTypeEnum.USER
                );

        this.update(new LambdaUpdateWrapper<EbMemberRegister>()
                .set(EbMemberRegister::getIsRegister,CommonConstant.ONE)
                .set(EbMemberRegister::getRegisterTime,ebMemberInfo.getCreateTime())
                .eq(EbMemberRegister::getId,memberRegister.getId()));
    }
}
